package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.common.Constant;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.domain.Collection;
import com.engineering.elink.service.CollectionService;
import com.engineering.elink.util.MapConvert;

@RestController
@RequestMapping("/api/collection")
@Api(value = "收藏接口")
public class CollectionController {
	@Autowired
	private CollectionService collectionService;
	
	/**
	 * 收藏
	 * @param userId
	 * @param thesisId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ApiOperation(value="用户收藏",httpMethod="POST")
	public Map<String, Object> update(@ModelAttribute @Valid Collection collection,HttpServletRequest request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			if((int)userInfo.get("userType")==Constant.personUserType){//只有个人用户才能够收藏
				collection.setUserId((String) userInfo.get("userId"));
				return 	collectionService.updateCollect(collection);
			}else{
				return MapConvert.getMap(ResultType.AUTHORITY_ERROR);
			}
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}
	
	
	/**
	 * 我的收藏
	 * @param count
	 * @return
	 */
	@RequestMapping(value="/myCollect",method=RequestMethod.GET)
	@ApiOperation(value="我的收藏文件")
	public Map<String, Object> getMyCollect(@ModelAttribute @Valid BaseCo co,
			@ApiParam(value="收藏类型1--博文，2--企业，3--职位,4--资料,5--论文,6--新闻,7--技能,8--课程,9--视频,默认1",required=true,defaultValue="1")
	@RequestParam(value="type",required=true,defaultValue="0")int type,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		return collectionService.getMyCollect(co,userId,type);
	}
}
